//订单信息
var redis=require('./Redis');


//根据foodId获取菜单数据
var getFoodInfoById=function(foods,foodId){
	for(var i=0;i<foods.length;i++){
		if(parseInt(foods[i].id)==parseInt(foodId)){
			return foods[i];
		}
	}
	return null;
}

//根据userId获取user数据
var getUserInfoById=function(users,userId){
	for(var i=0;i<users.length;i++){
		if(parseInt(users[i].id)==parseInt(userId)){
			return users[i];
		}
	}
	return null;
}

function Order(users,foods,uid,foodId,orderTime){
	var foodInfo=getFoodInfoById(foods,foodId);
	var user=getUserInfoById(users,uid);

	this.id=Order.id++;
	this.foodId=foodId;
	this.userId=uid;
	this.username=user.name;
	this.foodName=foodInfo.name;
	this.foodPrice=foodInfo.price;
	this.orderTime=orderTime;
}

//获取order列表
Order.getOrderList=function(callback){
	redis.keys("order:*", function (err, keys) {
		if(err) return callback(err);
        redis.mget(keys,function(error,orders){
        	var result=[],orders=orders||[];
            for(var i=0;i<orders.length;i++){
            	var order=JSON.parse(orders[i]);
            	var obj={};
    		    obj.id=keys[i].slice(6);//获取uid
    		    obj.order=order["order"];
    		    obj.time=order["time"];
    		    result.push(obj);
            }

            return callback(null,result);
        })
    });
}


//初始化单号
Order.id=1;

module.exports =Order;